package com.app;

import paskal.parsing.expression.InvalidExpressionException;
import paskal.parsing.expression.Parser;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Graph {
	
	private int width;
	private int height;
	private Paint paint;
	private Canvas canvas;
	private Parser parser;
	private String[] functions;
	private int[] colors={Color.BLUE,Color.RED};
	
	public Graph(Canvas canvas,Paint paint){
		width=canvas.getWidth();
		height=canvas.getHeight();
		this.paint=paint;
		this.canvas=canvas;
		parser=new Parser();
	}

	public boolean drawPlane(int x,int y, int odvid) {
		
		try{
		
		// from what pixel draw
		int tempx = x % odvid;
		int tempy = y % odvid;
		int fx = tempx;
		int fy = tempy;

		// draw lines
		paint.setColor(Color.LTGRAY);
		while (fx < width) {
			canvas.drawLine(fx, 0, fx, height, paint);
			fx += odvid;
		}
		while (fy < height - 50) {
			canvas.drawLine(0, fy, width, fy, paint);
			fy += odvid;
		}

		// draw numbers
		fx = tempx;
		fy = tempy;

		paint.setColor(Color.BLACK);
		
		while (fx < width) {
			if (fx - x != 0){
			canvas.drawText(Integer.toString((fx - x) / odvid), fx, y + 15,
					paint);	
			}else{
				canvas.drawText(Integer.toString((fx - x) / odvid), fx+7, y + 15,
						paint);		
			}
			fx += odvid;
		}
		while (fy < height - 50) {
			if (y - fy != 0){
			canvas.drawText(Integer.toString((y - fy) / odvid), x + 10, fy,
					paint);
			}
			fy += odvid;
		}

		// draw OX
		canvas.drawLine(0, y, width, y, paint);
		// draw OY
		canvas.drawLine(x, 0, x, height, paint);
		// draw ways
		canvas.drawLine(width, y, width - 8, y - 3, paint);
		canvas.drawLine(width, y, width - 8, y + 3, paint);
		canvas.drawLine(x, 0, x - 3, 8, paint);
		canvas.drawLine(x, 0, x + 3, 8, paint);
		

		// draw Graphics
		drawGraph(x,y,odvid);

		return true;
		}catch(Exception e){			
			e.printStackTrace();
			return false;
		}
	}

	private void drawGraph(int x,int y,int odvid) {
		if (functions != null) {
			for (int k = 0; k < functions.length; k++) {
				
				if (functions[k] != null && functions[k].length() > 0) {
					paint.setColor(colors[k]);

					try {
						parser.parseExpression(functions[k]);						
						for (int px = 0; px < width; px++) {
							parser.setVariable(Graph.pxToCoorX(px,x,odvid));
							int py = Graph.coorYToPx(parser.execute(),y,odvid);
							canvas.drawPoint(px, py, paint);

						}
					} catch (InvalidExpressionException e) {
						e.printStackTrace();
					}
					
				}
				paint.setColor(Color.BLACK);
			}
		}else{
			throw new NullPointerException();
		}
	}
	
	public void drawFingerCoordinates(Point currentFingerCoor,Point point,int odvid,Activity activity) {
		String xvalue = Double.toString(Graph.pxToCoorX((int) currentFingerCoor.x,point.x,odvid));
		String yvalue = Double.toString(Graph.pxToCoorY((int) currentFingerCoor.y,point.y,odvid));
		
		canvas.drawLine(currentFingerCoor.x-4,currentFingerCoor.y,currentFingerCoor.x+4,currentFingerCoor.y,paint);
		canvas.drawLine(currentFingerCoor.x,currentFingerCoor.y-4,currentFingerCoor.x,currentFingerCoor.y+4,paint);
		
		activity.setTitle("x="+xvalue+" \ty="+yvalue);
	}
	
	// pixels to coordinates
	public static double pxToCoorX(int px,float x,int odvid) {
		return ((double)(px - x) / odvid);
	}

	public static double pxToCoorY(int py,float y,int odvid) {
		return ((double)(y - py) / odvid);
	}

	// coordinates to pixels
	public static int coorXToPx(double coorx,float x,int odvid) {
		return (int) ((coorx * odvid) + x);
	}

	public static int coorYToPx(double coory,float y,int odvid) {
		return (int) (y - (coory * odvid));
	}

	public String[] getFunctions() {
		return functions;
	}

	public void setFunctions(String[] functions) {
		this.functions = functions;
	}
	
	public int[] getColors() {
		return colors;
	}

	public void setColors(int[] colors) {
		this.colors = colors;
	}

}
